package task

import "sync"

type taskMaps struct {
	data map[string]*TaskInfo
	mu   sync.RWMutex
}

func newTaskMaps() *taskMaps {
	m := new(taskMaps)
	m.data = make(map[string]*TaskInfo)
	m.mu = sync.RWMutex{}
	return m
}
func (s *taskMaps) Update(key string, fn func(task *TaskInfo)) bool {
	s.mu.Lock()
	defer s.mu.Unlock()
	if task, ok := s.data[key]; ok {
		fn(task)
		return true
	}
	return false
}
func (s *taskMaps) Load(key string) (*TaskInfo, bool) {
	s.mu.RLock()
	defer s.mu.RUnlock()
	task, ok := s.data[key]
	return task, ok
}
func (s *taskMaps) Store(key string, task *TaskInfo) {
	s.mu.Lock()
	s.data[key] = task
	s.mu.Unlock()
}
func (s *taskMaps) Size() int {
	s.mu.RLock()
	defer s.mu.RUnlock()
	return len(s.data)
}
func (s *taskMaps) Data() []*TaskInfo {
	var data []*TaskInfo
	s.mu.RLock()
	defer s.mu.RUnlock()
	for _, task := range s.data {
		data = append(data, task)
	}
	return data
}
func (s *taskMaps) Delete(key string) {
	s.mu.Lock()
	delete(s.data, key)
	s.mu.Unlock()
}

func (s *taskMaps) Reset() {
	s.mu.Lock()
	s.data = nil
	s.data = make(map[string]*TaskInfo)
	s.mu.Unlock()
}
